package com.puge.eduservice.controller;


import com.puge.commonutils.R;
import com.puge.eduservice.entity.EduChapter;
import com.puge.eduservice.entity.chapter.ChapterVo;
import com.puge.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */
@Api(description = "章节管理")//设置接口文档名称
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin  //跨域注解
public class EduChapterController {

//    将业务类接口以对象的方式注入
    @Autowired
    private EduChapterService chapterService;

//    课程大纲列表，根据课程id进行查询
    @GetMapping("getChapterVideo/{courseId}")
//    获取课程ID
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
//        将获取到的小节发返回
        return  R.ok().data("allChapterVideo",list);
    }

//    添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }

//    根据章节id查询
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
    EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }


//  修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }




//    删除的方法
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
    boolean flag = chapterService.deleteChapter(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }

    }

}

